 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    // typedef pair<int, int> PII;
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    int m, n, prev;

    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        // int prev = image[sr][sc];
        // if(color == prev) return image;
        // int m = image.size(), n = image[0].size();

        // queue<PII> q;
        // q.push({sr, sc});

        // while(!q.empty())
        // {
        //     auto [a, b] = q.front();
        //     image[a][b] = color;
        //     q.pop();

        //     for(int i = 0; i < 4; i++)
        //     {
        //         int x = a + dx[i], y = b + dy[i];
        //         if(x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev)
        //             q.push({x, y});
        //     }
        // }

        if (image[sr][sc] == color) return image;

        prev = image[sr][sc];
        m = image.size(), n = image[0].size();
        dfs(image, sr, sc, color);

        return image;
    }

    void dfs(vector<vector<int>>& image, int i, int j, int color)
    {
        image[i][j] = color;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev)
            {
                dfs(image, x, y, color);
            }
        }
    }
};

class Solution {
public:
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    vector<vector<bool>> vis;
    int m, n;

    int numIslands(vector<vector<char>>& grid) {
        m = grid.size(), n = grid[0].size();
        vis = vector<vector<bool>>(m, vector<bool>(n));
        int ret = 0;

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == '1' && !vis[i][j])//只要找到一个1，便从这个位置周围找其他的1
                {
                    ret++;
                    //标记这整块陆地
                    //bfs(grid, i, j);

                    dfs(grid, i, j);
                }
            }
        }

        return ret;
    }

    void bfs(vector<vector<char>>& grid, int i, int j)
    {
        queue<pair<int, int>> q;
        q.push({ i, j });
        vis[i][j] = true;

        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();

            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && !vis[x][y])
                {
                    q.push({ x, y });
                    vis[x][y] = true;
                }
            }
        }
    }

    void dfs(vector<vector<char>>& grid, int i, int j)
    {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == '1')
            {
                dfs(grid, x, y);
            }
        }
    }
};

class Solution {
public:
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    bool vis[51][51];
    int m, n;
    int count;


    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m = grid.size(), n = grid[0].size();
        int ret = 0;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == 1 && !vis[i][j])
                {
                    //ret = max(ret, bfs(grid, i, j));
                    count = 0;
                    dfs(grid, i, j);
                    ret = max(count, ret);
                }
            }
        }

        return ret;
    }

    int bfs(vector<vector<int>>& grid, int i, int j)
    {
        queue<pair<int, int>> q;
        q.push({ i, j });
        vis[i][j] = true;

        int count = 0;
        count++;
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();

            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y])
                {
                    q.push({ x, y });
                    count++;
                    vis[x][y] = true;
                }
            }
        }

        return count;
    }

    void dfs(vector<vector<int>>& grid, int i, int j)
    {
        vis[i][j] = true;
        count++;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y])
            {
                dfs(grid, x, y);
            }
        }
    }
};

class Solution {
public:
    int m, n;
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };

    void solve(vector<vector<char>>& board) {
        m = board.size(), n = board[0].size();

        //1.将边缘的O修改为.
        for (int i = 0; i < m; i++)
        {
            if (board[i][0] == 'O') //bfs(board, i, 0); 
                dfs(board, i, 0);
            if (board[i][n - 1] == 'O') //bfs(board, i, n - 1);
                dfs(board, i, n - 1);
        }

        for (int j = 0; j < n; j++)
        {
            if (board[0][j] == 'O') //bfs(board, 0, j);
                dfs(board, 0, j);
            if (board[m - 1][j] == 'O') //bfs(board, m - 1, j);
                dfs(board, m - 1, j);
        }

        //2.将.变回O，将O变换为X
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (board[i][j] == '.') board[i][j] = 'O';
                else if (board[i][j] == 'O') board[i][j] = 'X';
            }
        }
    }

    void bfs(vector<vector<char>>& board, int i, int j)
    {
        queue<pair<int, int>> q;
        q.push({ i, j });
        board[i][j] = '.';

        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O')
                {
                    q.push({ x, y });
                    board[x][y] = '.';
                }
            }
        }
    }

    void dfs(vector<vector<char>>& board, int i, int j)
    {
        board[i][j] = '.';
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O')
            {
                dfs(board, x, y);
            }
        }
    }
};

class Solution {
public:
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    int m, n;
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        m = heights.size(), n = heights[0].size();
        //可以流向太平洋的位置
        vector<vector<bool>> pac(m, vector<bool>(n));
        //可以流向大西洋的位置
        vector<vector<bool>> atl(m, vector<bool>(n));

        //1.先处理太平洋
        for (int i = 0; i < m; i++) dfs(heights, i, 0, pac);
        for (int j = 0; j < n; j++) dfs(heights, 0, j, pac);

        //2.再处理大西洋
        for (int i = 0; i < m; i++) dfs(heights, i, n - 1, atl);
        for (int j = 0; j < n; j++) dfs(heights, m - 1, j, atl);

        //总结所有可以流向太平洋和大西洋的位置
        vector<vector<int>> ret;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (pac[i][j] && atl[i][j])
                {
                    ret.push_back({ i, j });
                }
        return ret;
    }

    void dfs(vector<vector<int>>& heights, int i, int j, vector<vector<bool>>& vis)
    {
        vis[i][j] = true;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && heights[x][y] >= heights[i][j] && !vis[x][y])
            {
                dfs(heights, x, y, vis);
            }
        }
    }
};